package jp.sourceforge.akjrcp.ui;

import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

import jp.sourceforge.akjrcp.akj_betas.Messages;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;



public class EditableList extends TableViewer{
	public static Log log=LogFactory.getLog(EditableList.class);
	protected List<EditableListData> dataList=new ArrayList<EditableListData>();
	private MenuItem moveTop;
	private MenuItem moveBottom;
	private MenuItem moveUp;
	private MenuItem moveDown;
	private MenuItem removeData;
	
	private MenuItem editData;
	
	public class MenuSelection implements SelectionListener{

		public void widgetSelected(SelectionEvent e) {
			if(e.getSource()==addData){
				actionAdd();
			}else if(e.getSource()==editData){
				actionEdit();
			}else if(e.getSource()==moveTop){
				moveTop();
				}else if(e.getSource()==moveBottom){
				moveBottom();
				}else if(e.getSource()==moveUp){
				moveUp();
				}else if(e.getSource()==moveDown){
				moveDown();
				}else if(e.getSource()==removeData){
				removeSelection();
				}
			//getControl().redraw();
			refresh(true);
		}

		

		public void widgetDefaultSelected(SelectionEvent e) {
		}
		
	}
	private void actionEdit() {
		
	}

	private void actionAdd() {
		
	}
	public void removeAll(){
		dataList.removeAll(dataList);
		refresh(true);
	}
	
	public void removeSelection(){
		 log.info("remove");
	
		 EditableListData files[]=getSelectionDatas();
			for (int i = 0; i < files.length; i++) {
				dataList.remove(files[0]);
			}
			
	}
	public void moveUp(){
		EditableListData files[]=getSelectionDatas();
		
		if(files.length==1){
			int index=dataList.indexOf(files[0]);
			dataList.remove(files[0]);
			dataList.add(Math.max(0,index-1),files[0]);
		}
	}
	
	public void moveDown(){
		EditableListData files[]=getSelectionDatas();
		if(files.length==1){
			int index=dataList.indexOf(files[0]);
			dataList.remove(files[0]);
			dataList.add(Math.min(dataList.size(),index+1),files[0]);
		}
	}
	
	public void moveTop(){
		EditableListData files[]=getSelectionDatas();
		if(files.length==1){
			dataList.remove(files[0]);
			dataList.add(0,files[0]);
		}
	}
	
	public void moveBottom(){
		EditableListData files[]=getSelectionDatas();
		if(files.length==1){
			dataList.remove(files[0]);
			dataList.add(files[0]);
		}
	}
	
	public EditableList(Composite parent,int style){
		this(parent,style,false,false);
	}
	public void addEditListener(SelectionListener listener){
		editData.addSelectionListener(listener);
	}
	public void addAddListener(SelectionListener listener){
		addData.addSelectionListener(listener);
	}
	public EditableList(Composite parent,int style,boolean enableAdd,boolean enableEdit){
		super(parent,style);
		this.setLabelProvider(new ViewLabelProvider());
		this.setContentProvider(new ViewContentProvider());
		
		MenuSelection selection=new MenuSelection();
		menu = new Menu(getTable());
			getTable().setMenu(menu);
	        if(enableAdd){
	        addData = new MenuItem(menu, SWT.PUSH);
			addData.setText(Messages.getString("FileListTable.addfile")); //$NON-NLS-1$
	        addData.addSelectionListener(selection);
	        }
	        if(enableEdit){
		        editData = new MenuItem(menu, SWT.PUSH);
		        editData.setText(Messages.getString("FileListTable.editData")); //$NON-NLS-1$
		        editData.addSelectionListener(selection);
		        }
	        
	        MenuItem s1=new MenuItem(menu,SWT.SEPARATOR);
	        moveTop = new MenuItem(menu, SWT.PUSH);
	        moveTop.setText(Messages.getString("FileListTable.moveTop")); //$NON-NLS-1$
	        moveTop.addSelectionListener(selection);
	        
	        moveBottom = new MenuItem(menu, SWT.PUSH);
	        moveBottom.setText(Messages.getString("FileListTable.moveBottom")); //$NON-NLS-1$
	        moveBottom.addSelectionListener(selection);
	        
	        moveUp = new MenuItem(menu, SWT.PUSH);
	        moveUp.setText(Messages.getString("FileListTable.moveUp")); //$NON-NLS-1$
	        moveUp.addSelectionListener(selection);
	        
	        moveDown = new MenuItem(menu, SWT.PUSH);
	        moveDown.setText(Messages.getString("FileListTable.moveDown")); //$NON-NLS-1$
	        moveDown.addSelectionListener(selection);
	        MenuItem s2=new MenuItem(menu,SWT.SEPARATOR);
	        
	        removeData = new MenuItem(menu, SWT.PUSH);
	        removeData.setText(Messages.getString("EditableList.remove")); //$NON-NLS-1$
	        removeData.addSelectionListener(selection);
	        
	        getTable().addKeyListener(new KeyAdapter(){
	        	 public void keyReleased(KeyEvent event) {
	        		
	        	        if(event.keyCode==127){ //DELETE KEY
	        	            removeSelection();
	        	            refresh(true);
	        	        }
	        	       
	        	    }
	        });
	        

	        /*
	         * for dd
	        Transfer[] types = new Transfer[] {FileTransfer.getInstance()};
	    	
	    	int operations = DND.DROP_MOVE | DND.DROP_COPY ;
	    	
	    	
	    		DropTarget target2 = new DropTarget (getTable(), operations);
	    		target2.setTransfer(types);	
	    		*/
	    		this.getTable().setLinesVisible(true);
	        
	    		this.setInput(dataList);//some how it is very important
	}
	

	  
	  

	  /*
	public EditableList(Composite parent) {
		super(parent);
		
		this.setLabelProvider(new ViewLabelProvider());
		this.setContentProvider(new ViewContentProvider());
		
	}*/
	

	private MenuItem addData;
	private Menu menu;
	

	

	
	
            class ViewContentProvider implements IStructuredContentProvider {
        		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
        			
        		}
        		public void dispose() {
        		}
        		public Object[] getElements(Object parent) {
        			log.info("getElement");
        			return dataList.toArray();
        		}
        	}
            
        	class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
        		public String getColumnText(Object obj, int index) {
        			log.info("getColumnText:"+obj);
        			return ((EditableListData)obj).getLabel();
        		}
        		public Image getColumnImage(Object obj, int index) {
        			return null;
        		}

        	}
	
	
	




        	public void add(Object element) {
        		throw new RuntimeException("call addData:"+element);
        	}

	public void addData(EditableListData data){
		dataList.add(data);
		
		refresh(true);
	}
	
	public void addData(EditableListData data,int index){
		dataList.add(index,data);
		
		refresh(true);
	}



	public int size(){
		return dataList.size();
	}
	public EditableListData[] getDatas() {
		return (EditableListData[])dataList.toArray(new EditableListData[dataList.size()]);
	}
	
	
	public EditableListData[] getSelectionDatas() {
		ISelection selection=this.getSelection();
		return (EditableListData[]) ((IStructuredSelection)selection).toList().toArray(new EditableListData[0]);
	}
	
	public void selectData(int index){
		log.info("select:"+index);
		this.getTable().setSelection(index);
	}
	
	public void selectData(Object object){
		if(object==null){
			this.getTable().setSelection(-1);
			return;
		}
		EditableListData list[]=getDatas();
		for (int i = 0; i < list.length; i++) {
			if(list[i].getValue().equals(object)){
				this.getTable().setSelection(i);
				return;
			}
		}
	}
	
	public void selectData(EditableListData data){
		if(data==null){
			this.getTable().setSelection(-1);
			return;
		}
		EditableListData list[]=getDatas();
		for (int i = 0; i < list.length; i++) {
			if(list[i].equals(data)){
				this.getTable().setSelection(i);
				return;
			}
		}
	}
	
	public int countDatas(){
		return dataList.size();
	}
	
	public EditableListData popData(){
		if(dataList.size()>0){
			Object obj=dataList.get(0);
			dataList.remove(obj);
			refresh(true);
			return (EditableListData)obj;
		}else{
			return null;
		}
	}

	public Menu getMenu() {
		return menu;
	}
	
	
	
	public void addRemoveAllMenu(){
		new MenuItem(menu, SWT.SEPARATOR);
		MenuItem item = new MenuItem(menu, SWT.PUSH);
		item.setText(Messages.getString("FileListTable.remove_all")); //$NON-NLS-1$
		item.addSelectionListener(new SelectionListener(){

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				removeAll();
			}});
	}
}
